package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tianxin.workbao.workBao.Activity.BaseActivity;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.TransMoneyCalc;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.dataProvide.RoleProvide;
import com.tianxin.workbao.workBao.dataProvide.TransMoneyCalcProvide;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.utils.common.ToastUtils;
import com.tianxin.workbao.workBao.view.ExpandView;

import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class TransMoneyCalcAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<TransMoneyCalc> transMoneyCalcList;
    private Context mContext;
    private int mPosition;
    private String transSTr = "转账：%1$.3f\n银行：%2$s\n持卡人：%3$s \n账号：%4s\n转账编号：%5$s";
    private String ForceOilSTr = "加油：%1$.3f\n加油编号：%2$s";

    public TransMoneyCalcAdapter(List<TransMoneyCalc> list, Context context) {
        this.transMoneyCalcList = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (transMoneyCalcList.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.trans_money_clac_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            mPosition = position;
            viewHolder.BindViewData(transMoneyCalcList.get(position));
            viewHolder.setViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        if (transMoneyCalcList.size() > 0) {
            return transMoneyCalcList.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 0;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView arrowIV;
        ExpandView expandView;
        TextView numberTV;
        TextView statusLabelTV;
        TextView timeTV;
        TextView carTV;
        TextView calcWeightTV;
        TextView calcMoneyTV;
        TextView trueMoneyTV;
        TextView infoTV;
        TextView companyTV;
        TextView coalNameTV;
        TextView sendWeightTV;
        TextView receivedWeightTV;
        TextView lossWeightTV;
        TextView addUserTV;
        TextView oauthUserTV;
        TextView calcPriceTV;
        TextView lossValueTV;
        TextView outLossPriceTV;
        TextView outLossMoneyTV;
        TextView transferInforTV;
        TextView forceOilTV;
        LinearLayout lossLinerLayout;
        Button oauthBtn;


        public ViewHolder(View itemView) {
            super(itemView);
            numberTV = (TextView) itemView.findViewById(R.id.number);
            arrowIV = (ImageView) itemView.findViewById(R.id.arrow);
            expandView = (ExpandView) itemView.findViewById(R.id.expandView);
            statusLabelTV = (TextView) itemView.findViewById(R.id.status_label);
            timeTV = (TextView) itemView.findViewById(R.id.time);
            carTV = (TextView) itemView.findViewById(R.id.car_number);
            calcWeightTV = (TextView) itemView.findViewById(R.id.calc_weight);
            calcMoneyTV = (TextView) itemView.findViewById(R.id.calc_money);
            trueMoneyTV = (TextView) itemView.findViewById(R.id.true_money);
            infoTV = (TextView) itemView.findViewById(R.id.info);
            companyTV = (TextView) itemView.findViewById(R.id.company);
            coalNameTV = (TextView) itemView.findViewById(R.id.coal_name);
            sendWeightTV = (TextView) itemView.findViewById(R.id.send_weight);
            receivedWeightTV = (TextView) itemView.findViewById(R.id.received_weight);
            lossWeightTV = (TextView) itemView.findViewById(R.id.loss_weight);
            addUserTV = (TextView) itemView.findViewById(R.id.add_user);
            oauthUserTV = (TextView) itemView.findViewById(R.id.oauth_user);
            calcPriceTV = (TextView) itemView.findViewById(R.id.calc_price);
            lossValueTV = (TextView) itemView.findViewById(R.id.loss_value);
            outLossPriceTV = (TextView) itemView.findViewById(R.id.out_loss_price);
            outLossMoneyTV = (TextView) itemView.findViewById(R.id.out_loss_money);
            transferInforTV = (TextView) itemView.findViewById(R.id.transfer_info);
            forceOilTV = (TextView) itemView.findViewById(R.id.force_oil_info);
            lossLinerLayout = (LinearLayout) itemView.findViewById(R.id.loss_linerLayout);
            oauthBtn = (Button) itemView.findViewById(R.id.oauth_btn);
        }

        public void BindViewData(TransMoneyCalc transMoneyCalc) {
            numberTV.setText(transMoneyCalc.getNumber());
            if (transMoneyCalc.getBilltype() == 1) {
                companyTV.setText(transMoneyCalc.getReceivcompany());
            } else {
                companyTV.setText(transMoneyCalc.getSendcompany());
            }
            if (transMoneyCalc.getStatus() == 1) {
                statusLabelTV.setText("已审");
                timeTV.setText(TimeUtils.parseDateTime(transMoneyCalc.getAudittime()));
            } else {
                statusLabelTV.setText("未审");
                timeTV.setText("");
            }
            coalNameTV.setText(transMoneyCalc.getCoaltype());

            carTV.setText(transMoneyCalc.getPlatenumber() + " " + transMoneyCalc.getDriver());
            calcWeightTV.setText(transMoneyCalc.getCalcweight() + "吨");
            trueMoneyTV.setText("金额" + transMoneyCalc.getTruemoney());
            String info = "";
            //路耗类型 1不计路耗 2 减合理扣吨 3超出金扣
            info += transMoneyCalc.getLosttype() == 1 ? "不计路耗"
                    : (transMoneyCalc.getLosttype() == 2 ? "减合理扣吨"
                    : (transMoneyCalc.getLosttype() == 3 ? "超出金扣" : ""));
            info += (transMoneyCalc.getTranspaytype() == 1 ? " 收货净重 " : " 发货净重 ");
            info += (transMoneyCalc.getBilltype() == 1 ? " 出库 " : " 入库 ");
            info += (transMoneyCalc.getIsaddtax() == 0 ? " 不含税" : " 含税");
            infoTV.setText(info);
            sendWeightTV.setText("原发" + transMoneyCalc.getSendweight() + "吨");
            receivedWeightTV.setText("收货" + transMoneyCalc.getReceiveweight() + "吨");
            lossWeightTV.setText("磅差" + transMoneyCalc.getLostweight() + "吨");
            if (transMoneyCalc.getLosttype() == 1) {
                lossLinerLayout.setVisibility(View.GONE);
            } else {
                lossLinerLayout.setVisibility(View.VISIBLE);
                lossValueTV.setText("路耗标准 " + transMoneyCalc.getLostvalue());
                outLossPriceTV.setText("超损价 " + transMoneyCalc.getLostunitpirce());
                outLossMoneyTV.setText("超损价 " + transMoneyCalc.getLostmoney());
            }
            calcPriceTV.setText("单价 " + transMoneyCalc.getUnitprice());
            calcWeightTV.setText("结算 " + transMoneyCalc.getCalcweight() + " 吨");
            calcMoneyTV.setText("结算 " + transMoneyCalc.getCalcmoney() + " 元");
            addUserTV.setText("添加：" + transMoneyCalc.getAddusername());
            //"转账：{0}\\n银行：{1}\\n持卡人：{2} \\n账号：{3}\\n转账编号：{4}";
            transferInforTV.setText(String.format(transSTr,
                    transMoneyCalc.getTransferMoney(),
                    transMoneyCalc.getBnakName(),
                    transMoneyCalc.getCardholder(),
                    transMoneyCalc.getBnakAccount(),
                    transMoneyCalc.getTransferMoneyNumber()));
            forceOilTV.setText(String.format(ForceOilSTr,
                    transMoneyCalc.getOilMoney(),
                    transMoneyCalc.getOilBillNumber()));

            if (transMoneyCalc.getStatus() == 1) {
                oauthUserTV.setText("审核：" + transMoneyCalc.getAuditusername() + "\n"
                        + TimeUtils.parseDateTime(transMoneyCalc.getAudittime()));
                oauthBtn.setVisibility(View.GONE);
            } else {
                oauthUserTV.setText("未审核");
                oauthUserTV.setTextColor(mContext.getResources().getColor(R.color.red_btn_bg_color));
                RoleProvide roleProvide = new RoleProvide(mContext, null);
                if (roleProvide.hasRole(Constant.ROLE_TRANS_FREIGHT_CLERK_OAUTHER)) {
                    oauthBtn.setVisibility(View.VISIBLE);
                }

            }
        }

        public void setViewListener(final int position) {
            arrowIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (expandView.isExpand()) {
                        expandView.collapse();
                        arrowIV.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp);
                    } else {
                        expandView.expand();
                        arrowIV.setImageResource(R.drawable.ic_arrow_drop_up_black_24dp);
                    }
                }
            });
            oauthBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (view.getId()) {
                        case R.id.oauth_btn:
                            TransMoneyCalcProvide provide = new TransMoneyCalcProvide(mContext, new UpdateListViewListener() {
                                @Override
                                public void onRefreshData(Object result) {

                                }

                                @Override
                                public void onLoadMoreData(Object result) {

                                }

                                @Override
                                public void showMessage(String msg) {
                                    if (msg == "ok") {
                                        expandView.collapse();
                                        transMoneyCalcList.remove(position);
                                        notifyDataSetChanged();
                                        ToastUtils.showLong("操作成功");
                                    } else if (msg.contains("failure")) {
                                        ToastUtils.showLong("操作失败！\n " + msg.substring(msg.indexOf("failure") + 1, msg.length()));
                                    } else {
                                        ToastUtils.showLong(msg);
                                    }
                                }

                                @Override
                                public void showProgress() {
                                    BaseActivity activity = (BaseActivity) TransMoneyCalcAdapter.this.mContext;
                                    activity.getSweetAlertDialog(activity).showDialog("", 1200);
                                }

                                @Override
                                public void hideProgress() {
                                    BaseActivity activity = (BaseActivity) TransMoneyCalcAdapter.this.mContext;
                                    activity.getSweetAlertDialog(activity).dismiss();
                                }
                            }, null);
                            provide.oauth(transMoneyCalcList.get(position));
                            break;
                        default:
                            break;
                    }
                }
            });
        }

    }


}
